// 医技工作站
<template>
  <el-row class="medical-body">
    <el-col :span="24">
      <TopManu></TopManu>
    </el-col>
    <el-row class="medical-content" >
      <el-col :span="24" style="marginTop:10px;display:flex;">
        <div class="tabs" @click="active = 1"  :class="active == 1?'active':''">门诊</div>
        <div class="tabs" @click="active = 2" :class="active == 2?'active':''">住院</div>
      </el-col>
      <el-col class="search">
        <el-col :span="24" class="search-input">
          <el-col :span="5">
            <el-input v-model="keyValue" placeholder="请输入名称"></el-input> 
          </el-col>
          <el-col :span="4" :offset="1">
            <el-button style="height:40px;" type="primary" @click="handleSerch">查询</el-button>
          </el-col>
          <el-col :span='2' style="marginLeft:auto;">
            <el-button style="height:40px;" @click="handleExecute" type="primary">全部执行</el-button>
          </el-col>
        </el-col>
        <el-col :span="24" class="search-select" >
               <el-select
              v-model="type"
              style="marginRight:10px;"
              placeholder="请选择状态"
              >
                <el-option
                  v-for="item in statsOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
              <!-- <el-select
                v-model="departments"
                style="marginRight:10px;"
                placeholder="请选择执行科室"
              >
                <el-option
                  v-for="item in arr"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select> -->
              <el-date-picker
              v-model="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
        </el-col>
      </el-col>
      <el-col :span="24" style="marginTop:20px;">
        <FromList
          @handleCurrentChange="handleCurrentChangeSchool"
          @handleCommand="handleCommand"
          :tableData="tableData"
          :tableTitle="tableTitle"
          :typeCheckbox="true"
          :height="500"
          :activityFlag="true"
          @handelChange="handelChangeMask"
        />
      </el-col>
      
      <el-col :span="24" style="marginTop:20px;" class="flex-right">
        <el-pagination
          background
          :current-page="page"
          :page-size="limit"
          layout="total , prev, pager, next , jumper"
          @current-change="handlePagination"
          :total="userCount"
        ></el-pagination>
      </el-col>
    </el-row>
    <div class="dialog">
       <el-dialog
          :close-on-click-modal="false"
          title="录入"
          center
          :visible.sync="flag"
          width="30%"
        >
          <el-col :span="24" style="margin:0 0 20px 0px;">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入录入内容"
              :disabled="flagText==2"
              v-model="textarea">
            </el-input>
          </el-col>
          <div
            slot="footer"
            class="dialog-footer"
            style="padding: 0px; margin: 5px"
          >
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="success" :disabled="flagText==2" @click="handleEntering"
              >确定</el-button
            >
          </div>
        </el-dialog>
    </div>
  </el-row>
</template>

<script>
import FromList from "@/forComponents/fromList/fromList"
import TopManu from "@/components/TopMenu"
import {tableTitle} from "./titleTitle"
import {orderIndex,service_checkExecute,service_checkCheck,saveResult,getHospitalOrderIndex,setExecuteHospitalOrder,setSaveHospitalResult,checkHospitalReport} from "@/api/medical.js"
export default {
  data(){
    return {
      active:1,
      arr:[],
      type:0,
      departments:1,
      date:"",
      keyValue:"",
      tableTitle:tableTitle,
      id:"",
      tableData:[],
      page:1,
      limit:20,
      userCount:0,
      executeList:[],
      textarea:"",
      flag:false,
      flagText:1,
      statsOptions:[
        {
          name:"全部",
          id:0
        },
        {
          name:"待执行",
          id:1
        },
        {
          name:"已执行",
          id:2
        }
      ]
    }
  },
  created(){
    this.orderIndex()
  },
  watch:{
    active(e){
      this.page = 1
      this.keyValue = ""
      this.type = 0
      this.date = ""
      this.tableData = []
      if(e == 1) this.orderIndex()
      else this.getHospitalOrderIndex()
    }
  },
  methods:{
    handleSerch(e){
      this.page = 1
      this.tableData = []
      if(this.active == 1) this.orderIndex()
      else this.getHospitalOrderIndex()
    },
    getHospitalOrderIndex(){
       let date
      if(this.date != ""){
        date = this.date.join("|")
      }else{
        date = ""
      }
      console.log("this.keyValue",this.keyValue)
      getHospitalOrderIndex({page:this.page,limit:this.limit,keyword:this.keyValue,do_time:date,status:this.type})
      .then(res=>{
        console.error(res)
        if(res.code == 200 && res.data.list.length != 0){
          let {list:data} = res.data
          data.forEach((item,key)=>{
            item.index = key + 1 
            item.statusFlag = item.status == 1 ? '未执行' : '已执行'
            if(item.member){
              item.memberName = item.member.name
              item.memberId_card = item.member.id_card
            }
            if(item.service_info){
               item.service_infoNumber = item.service_info.number
            item.service_infoName = item.service_info.name 
            item.service_infoCheck_sample = item.service_info.check_sample
            // item.doctorReal_name = item.doctor.real_name
            // item.doctorDept_name = item.doctor.dept_name
            item.service_infoExecute_dept = item.service_info.execute_dept
            item.service_infoPrice = item.service_info.price
            item.service_infoUnit = item.service_info.unit
            }
           
            
            if(item.execute_user){
               item.execute_userName = item.execute_user
            }
          })
          this.userCount = res.data.count
          this.tableData = data
        }
      })
      .catch(e=>{
        console.log(e)
      })
    },
    service_checkCheck(e){
      if(this.active == 1){
         service_checkCheck({type:1,id:e})
        .then(res=>{
          if(res.code == 200){
            this.$message({
              message: res.message,
              type: "success",
            });
            this.tableData = []
            this.orderIndex() 
          }
        })
      }else{
        console.log("进入")
        checkHospitalReport({type:1,id:e})
        .then(res=>{
          if(res.code == 200){
            this.$message({
              message: res.message,
              type: "success",
            });
            this.tableData = []
            this.getHospitalOrderIndex() 
          }
        })
      }
    },
    service_checkExecute(e){
      if(this.active == 1){
         service_checkExecute({id:e})
        .then(res=>{
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.tableData = []
          this.orderIndex()
        })
      }else{
        setExecuteHospitalOrder({id:e})
        .then(res=>{
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.tableData = []
          this.getHospitalOrderIndex()
        })
      }
      
    },
    handleClose(){
      if(this.flagText == 1) this.flag = false
      else {this.textarea = "",this.flag = false}
    },
    orderIndex(){
      let date
      if(this.date != ""){
        date = this.date.join("|")
      }else{
        date = ""
      }
      orderIndex({page:this.page,limit:this.limit,keyword:this.keyValue,do_time:date,status:this.type})
      .then(res=>{
        if(res.code == 200 && res.data.list.length != 0){
          let {list:data} = res.data
          data.forEach((item,key)=>{
            item.index = key + 1 
            item.statusFlag = item.status == 1 ? '未执行' : '已执行'
            if(item.member){
              item.memberName = item.member.name
              item.memberId_card = item.member.id_card
            }
            
            item.service_infoNumber = item.service_info.number
            item.service_infoName = item.service_info.name 
            item.service_infoCheck_sample = item.service_info.check_sample
            item.doctorReal_name = item.doctor.real_name
            item.doctorDept_name = item.doctor.dept_name
            item.service_infoExecute_dept = item.service_info.execute_dept
            item.service_infoPrice = item.service_info.price
            item.service_infoUnit = item.service_info.unit
            
            if(item.execute_user){
               item.execute_userName = item.execute_user.real_name
            }
          })
          this.userCount = res.data.count
          this.tableData = data
        }
      })
      .catch(e=>{
        console.log(e)
      })
    },
    handleExecute(){
      if(this.executeList.length != 0){
        let data = []
        this.executeList.forEach(item=>{
          data.push(item.id)
        })
        this.service_checkExecute(data.join(","))
      }else{
        this.$message({
          message: "请先选择需要执行的内容",
          type: "error",
        });
      }
    },
    handleCurrentChangeSchool(e){
      console.log(e)
      
    },
    handleCommand(e){
      console.log(e)
      switch(e.key){
        case 1:
          if(e.data.status == 1){
            this.service_checkExecute(String(e.data.id))
          }else{
            this.$message({
              message: "当前处方不可执行",
              type: "error",
            });
          }
        break
        case 2:
          if(e.data.status == 2){
              this.flagText = 1
              this.flag = true
              this.id = e.data.id
          }else{
            this.$message({
              message: "该单据未执行",
              type: "error",
            });
          }
        break
        case 3:
          console.log(e.data.check_result)
          if(e.data.check_result){
            this.flagText = 2
            this.flag = true
            this.textarea = e.data.check_result
            this.id = e.data.id
          }else{
            this.$message({
              message: "还未录入结果",
              type: "error",
            });
          }
        break
        case 4:
          if(e.data.status == 2){
            if(e.data.check_result){
              this.service_checkCheck(e.data.id)
            }else{
              this.$message({ 
                message: "还未录入结果",
                type: "error",
              });
            }
          }else{
            this.$message({
              message: "该单据未执行",
              type: "error",
            });
          }
         
        break
      }
    },
    handelChangeMask(e){
      this.executeList = e
    },
    handlePagination(e){
      this.page = e
      this.tableData = []
      this.orderIndex()
    },
    handleEntering(){
      if(this.active == 1){
        saveResult({id:this.id,check_result:this.textarea})
        .then(res=>{
          console.log(res,"录入结果")
          if(res.code && res.code == 200){
            this.$message({
              message: res.message,
              type: "success",
            });
          }
          this.textarea = ""
          this.tableData = []
          this.orderIndex()
          this.flag = false
        })
      }else{
        setSaveHospitalResult({id:this.id,check_result:this.textarea})
        .then(res=>{
          console.log(res)
          if(res.code && res.code == 200){
            this.$message({
              message: res.message,
              type: "success",
            });
          }
          this.textarea = ""
          this.tableData = []
          this.getHospitalOrderIndex()
          this.flag = false
        })
      }
    }
  },
  components:{
    TopManu,
    FromList
  }
}
</script>

<style lang="scss" scoped>
  @import "./index.scss";
</style>